<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
    <script src="./utils/getFeatureGeom.js"></script>
    <script src="./utils/createDraw.js"></script>
    <script src="./utils/LineString.js"></script>
</head>

<body>
    <button onclick="activeDraw()">激活画笔</button>
    <div id="map">
    </div>
    <script>
        /* 地图文档 */
        var docLayer = new Zondy.Map.Doc("", "myTestMap", {

        })
        var map = new ol.Map({
            target: "map",
            layers: [gaode, docLayer],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 4
            })
        })
        /* 1、通过画笔获取坐标 */
        let source = new ol.source.Vector({

        })
        let layer = new ol.layer.Vector({
            source
        })
        map.addLayer(layer)

        function activeDraw() {
            let draw = createDraw({
                type: "LineString",
                source
            })
            map.addInteraction(draw)
            draw.on("drawend", evt => {
                // source.clear()
                setTimeout(() => {
                    source.clear()
                })
                // console.log(1);
                let position = getFeatureGeom(evt.feature);
                console.log(position, 'position');// 二维数组
                let attr = [{ name: "铁路长度", type: "string", value: "1008" + Date.now() }]
                // 添加属性的时候，可以传递空数组
                // let attr = []
                var service = {
                    name: "myTestMap",
                    layerId: 2
                }
                LineString.add({
                    position,
                    attr,
                    service,
                    docLayer
                })
            })
        }
    </script>
</body>

</html>